package org.vectrics.recruiter.reporting;

import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.BaseFont;
import org.apache.log4j.Logger;
import org.vectrics.VectricsException;
import org.vectrics.common.reporting.pdf.ReportUtil;
import org.vectrics.domain.Domain;

import java.util.Iterator;
import java.util.Locale;
import java.util.TreeSet;
import java.text.*;

import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.*;



public class SkillSetupReport extends RecruitBaseReport {
	private transient static Logger log = Logger.getLogger(SkillSetupReport.class);
	private PdfPTable mainTable = new PdfPTable(6);
	
	
	
	public SkillSetupReport(Domain domain, Locale locale) {
		super(domain, locale);
		try {
//   		FOOTER_FONT = 
//				BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		}
		catch (Exception de) {
			log.error("Exception creating footer font", de);
		}
	}

	@Override
	protected Rectangle getPageSize() {
		return(PageSize.A4.rotate());
	}


	 
	@Override
	protected void fill(Document document) throws VectricsException {
		log.debug("Skill Setup Report - building");
		try {
			
			document.add(new Paragraph("Skill Setup", ReportConstants.titleFont));

			log.debug("fill() - setting table properties");
			mainTable.setHorizontalAlignment(Element.ALIGN_LEFT);
			mainTable.setWidthPercentage(100);
			int mainTableWidths[] = {100, 20, 20, 20, 20, 20};
			mainTable.setWidths(mainTableWidths);

			log.debug("fill() - adding table header row");
			addTableHeaderCell("Description");
			addTableHeaderCell("Code");
			addTableHeaderCell("Needs Level");
			addTableHeaderCell("Needs Rating");
			addTableHeaderCell("Needs Years");
			addTableHeaderCell("Needs Yes/No");
			mainTable.setHeaderRows(1);
			
			log.debug("fill() - adding job rows");
			Skill rootSkill = RecruitServiceLocator.getSkillProvider().getRootSkill();
			displaySkillBranch(mainTable, rootSkill, 0);
			log.debug("fill() - adding job table to document");
			document.add(mainTable);

		}
		catch(Exception e) {
			log.error("Exception creating pdf document - in fill()", e);
			throw new VectricsException("Exception creating pdf document - fill()", e);
		}
        
	}
	
	private String getIndentStr(int level) {
		String indentStr = "";
		for (int i = 0; i < level; i++) {
			indentStr += " * ";
		}
		return(indentStr);
	}

	protected void displaySkill(PdfPTable table, Skill parentSkill, int level) throws VectricsException {
		try {
			Color color = new Color(255, 255, 255);
			
			log.debug("addJobRow() - called");
			
			log.debug("addJobRow() - displaying date...");
			PdfPCell cell = null;
			if (parentSkill.getSection()) {
				cell = new PdfPCell(ReportUtil.createPhrase(getIndentStr(level) + parentSkill.getDescription(), ReportConstants.smlBoldFont));
			}
			else {
				cell = new PdfPCell(ReportUtil.createPhrase(getIndentStr(level) + parentSkill.getDescription(), ReportConstants.smlFont));
			}
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);	
			
			log.debug("addJobRow() - displaying reference id...");
			cell = new PdfPCell(ReportUtil.createPhrase(parentSkill.getCode(), ReportConstants.smlFont));
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);	

			cell = new PdfPCell(ReportUtil.createPhrase("" + parentSkill.getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_LEVEL), ReportConstants.smlFont));
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);

			
			cell = new PdfPCell(ReportUtil.createPhrase("" + parentSkill.getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_LEVEL), ReportConstants.smlFont));
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);	
			/*
			log.debug("addJobRow() - displaying needs year...");
			cell = new PdfPCell(ReportUtil.createPhrase("" + parentSkill.getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_YEARS), ReportConstants.smlFont));
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);	
			*/
			
			cell = new PdfPCell(ReportUtil.createPhrase("" + parentSkill.getScoringMethod().getCode().equals(SkillScoringMethod.SCORING_METHOD_YES_NO), ReportConstants.smlFont));
			cell.setBackgroundColor(color);
			this.mainTable.addCell(cell);
		}
		catch (Exception ex) {
			log.error("Exception caught while adding skill row = " + parentSkill.getCode(), ex);
			throw new VectricsException("Exception caught while adding job row for job = " + parentSkill.getCode(), ex);
		}	
	}   
	
	
	
	void displaySkillBranch(PdfPTable table, Skill parentSkill, int level) throws VectricsException {
		displaySkill(table, parentSkill, level);

		TreeSet children = new TreeSet<Skill>(new SkillChildComparator());
		children.addAll(parentSkill.getChildren());
		
		Iterator iter =  children.iterator();
		while (iter.hasNext()) {
			Skill child = (Skill)iter.next();
			displaySkillBranch(table, child, level + 1);
		}
	}
	
    
	protected void setupHeaderFooter(Document document) throws VectricsException {
	}


	protected void addTableHeaderCell(String title) {
		log.debug("addTableHeaderCell()");
		PdfPCell hdrCell = new PdfPCell(new Phrase(title, ReportConstants.stdBoldFont));
		hdrCell.setBackgroundColor(ReportConstants.TABLE_HEAD_COLOR);
		this.mainTable.addCell(hdrCell);	
	}
	   

	

	

}
